package com.vf.cloud.paas.app.master.api.dashboard;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.paas.app.master.common.domain.Asset;
import com.vf.cloud.paas.app.master.common.domain.Product;
import com.vf.cloud.paas.app.master.common.domain.Project;
import com.vf.cloud.paas.app.master.common.domain.RenderAgent;
import com.vf.cloud.paas.app.master.common.domain.RenderAgentGpu;
import com.vf.cloud.paas.app.master.common.domain.Scene;
import com.vf.cloud.paas.app.master.common.vo.render.RenderOrderP;
import com.vf.cloud.paas.common.log.annotation.RLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.service.IRedis;
import com.vf.cloud.paas.common.util.R;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/dashboard")
public class DashboardController {

	@Autowired
	private IRedis redisImpl;

	@RLog(value = "工作台-数据卡片", type = "user")
	@RequestMapping(value = "/cardInfo", method = RequestMethod.POST)
	public R<Map<String, Double>> cardInfo(@RequestBody RenderOrderP orderP) {

		double projects = Db.queryDouble("select count(id)  from " + Project.TABLE_NAME);
		double scenes = Db.queryDouble("select count(id) from " + Scene.TABLE_NAME);
		double assets = Db.queryDouble("select count(id) from " + Asset.TABLE_NAME);
		double products = Db.queryDouble("select count(id) from " + Product.TABLE_NAME);

		Map<String, Double> cards = new HashMap<String, Double>();
		cards.put("projects", projects);
		cards.put("scenes", scenes);
		cards.put("assets", assets);
		cards.put("products", products);
		return R.ok(cards);

	}

	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<RenderAgent>> pages(HttpServletRequest request,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " from " + RenderAgent.TABLE_NAME + "  a WHERE 1=1 ";
		sqlExceptSelect += "  ORDER BY a.name  , a.ip desc ";
		Page<RenderAgent> pages=RenderAgent.dao.paginate(cur, limit, "select * ", sqlExceptSelect);
		for(RenderAgent agent:pages.getList()) {
			List<RenderAgentGpu> gpus=RenderAgentGpu.dao.find("select * from "+RenderAgentGpu.TABLE_NAME+" where agent_id=?",agent.getId());
			
			int  renderNumber=0;
			int  renderNumberFree=0;
			int  renderNumberUsed=0;
			for(RenderAgentGpu g:gpus) {
				Set<String> t=redisImpl.getByUuidPrefix(g.getId());
				Set<String> f=redisImpl.getByUuidPrefix("0",g.getIndex()+"_"+g.getId());
				Set<String> u=redisImpl.getByUuidPrefix("1",g.getIndex()+"_"+g.getId());
				
				renderNumber+=t.size();
				renderNumberFree+=f.size();
				renderNumberUsed+=u.size();
			}
			agent.setRenderNumber(renderNumber);
			agent.setRenderNumberFree(renderNumberFree);
			agent.setRenderNumberUsed(renderNumberUsed);
		}
		return R.ok(pages);
	}

}
